<?php
/**
 * Administracja - lista przedmiotów
 * @author Miron Pawlik
 */

//sprawdzanie czy formularz byl wyslany
if($fd['editData']) {

    $errorMsg = "";
    foreach($fd['name'] as $key => $name) {
        $errorMsgLocal = "";
        if(strlen($fd['name'][$key]) < 2)
            $errorMsgLocal = "Podaj nazwę ($name)<br/>";
        if(strlen($fd['groups'][$key]) < 1)
            $errorMsgLocal = "Podaj liczebność grupy ($name)<br/>";


        //edycja danych przedmiotu
        if(!$errorMsgLocal) {
            $db->editById($fd['subjectID'][$key], 'subjects', array('name'=> $fd['name'][$key], 'groups'=> $fd['groups'][$key], 'color'=>$fd['color'][$key], 'room' => $fd['room'][$key], 'day' => $fd['day'][$key], 'week' => $fd['week'][$key], 'start' => $fd['start'][$key], 'end' => $fd['end'][$key], 'teacher' => $fd['teacher'][$key]), 'subjectID');
        }
        else {
            $errorMsg .= $errorMsgLocal;
        }
    }
    if(!$errorMsg) {
        header("Location: ".$_conf['host'].'?admin,subjects');
    }
}
if($fd['type'] == 'add') {
    //walidacja danych
    if(strlen($fd['name']) < 2)
        $errorMsg = "Podaj nazwę";
    if(strlen($fd['groups']) < 1)
        $errorMsg = "Podaj liczebność grupy <br/>";

    if(!$errorMsg) {

        $db->addNew('subjects', array('name' => $fd['name'], 'groups' => $fd['groups'], 'color' => $fd['color'], 'room' => $fd['room'], 'day' => $fd['day'], 'week' => $fd['week'], 'start' => $fd['start'], 'end' => $fd['end'], 'teacher' => $fd['teacher']));
        header("Location: ".$_conf['host'].'?admin,subjects');
    }
}
//usuwanie przedmiotu
if($fd[2] == 'del') {
    //sprawdzanie czy pzredmiot nie ma zdefiniowanych jakis terminow (nie mozna go wtedy usunac)
    if(!$db->CountRec('terms', 'subjectID = '.(int)$fd[3])) {
        $db->delById($fd[3], 'subjects', 'subjectID');
        header("Location: ".$_conf['host'].'?admin,subjects');
    }
    else
        $errorMsg = "Nie można usunąć przedmiotu majacego jakieś terminy";

}

$all = $db->getAll('subjects', 'name');


$smarty->assign('subjects', $all);

$colors = array('#00008B',
        '#654321',
        '#08457E',
        '#986960',
        '#CD5B45',
        '#B8860B',
        '#013220',
        '#BDB76B',
        '#8B008B',
        '#03C03C',
        '#E75480',
        '#560319',
        '#E9967A',
        '#2F4F4F',
        '#177245',
        '#918151',
        '#00CED1',
        '#9400D3',
        '#555555',
        '#EF3038',
        '#DA3287',
        '#B94E48',
        '#C154C1',
        '#9955BB',
        '#CD00CC',
        '#FFCBA4',
        '#FF1493',
        '#FF9933',
        '#1560BD',
        '#EDC9AF',
        '#1E90FF',
        '#00009C');

$smarty->assign('colors', $colors);
$smarty->assign('hours', $hours);
$smarty->assign('week', $weeks);
$smarty->assign('days', $days);


